<?php
namespace app\index\controller;
use app\index\controller\Phpqrcode;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;
/**
 * 
 */
class Test extends Controller
{

    public  function  test()
    {
        $qrcode = new PHPqrcode;
        $content = $file = 'http://erp.xiangyudan.cn';
        $path = $qrcode->getqrcode($content);
       return $path;
    }

     public function rank()
     {
     	$list = DB::name('order')
                    ->alias('a')
                    ->join('user b','a.author = b.id')
                    ->field('b.truename,SUM(a.true_price)as total,count(a.order_num)')
                    ->where("a.is_del = 0 and DATE_FORMAT(FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%s'), '%m-%d') >'09-01' and DATE_FORMAT(FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%s'), '%m-%d')< '10-01'")
                    ->group('a.user_id')
                    ->order('total desc')
                    ->select();
        var_dump(DB::name('order')->getLastSql()).die();
     }

    public function biao()
    {
        $name='定制订单列表';
        $header=['序号','客户','日期','品类','价格','订单属性','客户来源','新老客户','顾问','所属店铺'];
        for($i=0;$i<3;$i++){
            $str[] =[1,1,1,1,1,1,1,1,1,'<img src="'.$this->test().'"/>'];

        }
        $data=$str;
        excelExport($name,$header,$data);
    }

    public function ExcelDemo(){
        //demo测试
        //数据库查询的数据
        $data=db('fabric')->select();
        //tp5 的vendor引用
        vendor('phpoffice.phpexcel.Classes.PHPExcel');
        vendor('phpoffice.phpexcel.Classes.PHPExcel.Writer.IWriter');
        vendor('phpoffice.phpexcel.Classes.PHPExcel.Writer.Abstract');
        vendor('phpoffice.phpexcel.Classes.PHPExcel.Writer.Excel5');     //excel 为2003版本
        vendor('phpoffice.phpexcel.Classes.PHPExcel.Writer.Excel2007');   //excel 为2007版本 可以自己任选一个
        vendor('phpoffice.phpexcel.Classes.PHPExcel.Writer.Excel2007.Drawing');
        vendor("phpoffice.phpexcel.Classes.PHPExcel.IOFactory");
        $objPHPExcel = new \PHPExcel();
        //$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);

        $objActSheet = $objPHPExcel->getActiveSheet();

        //设置首行的 各列的名称
        $objActSheet->setCellValue('A1', 'id')
            ->setCellValue('B1', '面料编号')
            ->setCellValue('C1', '品类')
            ->setCellValue('D1', '属性')
            ->setCellValue('E1', '面料成分')
            ->setCellValue('F1', '面料花色')
            ->setCellValue('G1', 'leibie')
            ->setCellValue('H1', '颜色')
            ->setCellValue('I1', '纱织')
            ->setCellValue('J1', '总库存')
            ->setCellValue('K1', '剩余库存')
            ->setCellValue('L1', '图片')
            ->setCellValue('M1', '二维码')
            ->setCellValue('N1', '价格')
            ->setCellValue('O1', '操作者')
            ->setCellValue('P1', '录入时间');

        // 设置个表格宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(16);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(12);
        foreach($data as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k, $v['id']);
            $objActSheet->setCellValue('B'.$k, $v['ml_num']);
//            $objActSheet->setCellValue('C'.$k, $v['content']);
            if(strlen($v['codeurl'])>0){
                $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
                $objDrawing[$k]->setPath('.'.$v['codeurl']);//这里拼接 . 是因为要在根目录下获取
                // 设置宽度高度
                $objDrawing[$k]->setHeight(80);//照片高度
                $objDrawing[$k]->setWidth(80); //照片宽度
                /*设置图片要插入的单元格*/
                $objDrawing[$k]->setCoordinates('M'.$k);
                // 图片偏移距离
                $objDrawing[$k]->setOffsetX(2);
                $objDrawing[$k]->setOffsetY(12);
                $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
                // 表格高度
                $objActSheet->getRowDimension($k)->setRowHeight(80);
            }
            if(strlen($v['picurl'])>0){
                $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
                $objDrawing[$k]->setPath('.'.$v['codeurl']);//这里拼接 . 是因为要在根目录下获取
                // 设置宽度高度
                $objDrawing[$k]->setHeight(80);//照片高度
                $objDrawing[$k]->setWidth(80); //照片宽度
                /*设置图片要插入的单元格*/
                $objDrawing[$k]->setCoordinates('L'.$k);
                // 图片偏移距离
                $objDrawing[$k]->setOffsetX(2);
                $objDrawing[$k]->setOffsetY(12);
                $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
                // 表格高度
                $objActSheet->getRowDimension($k)->setRowHeight(80);
            }
            /*else{
                $objActSheet->setCellValue('C'.$k, $this->test());
            }*/
        }
        $fileName = '报价表';
        $date = date("Y-m-d",time());
        $fileName .= "_{$date}.xls";
        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        // $objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载

    }

}